// console.log(getApp().globalData)
var axios = require("../util/request.js")
var app = getApp()
// console.log(app)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dayStyle: [
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' },
      { month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5' }
    ],
    token: '',
    newday: '',
    startTime: '',
    endTime: '',
    type: '0',
    data: '',
    hiddenmodalput: false,
    hiddenmodalput1: false,
    time: '',
    amount: '',
    num: '',
    tempimg: '',
    tempimg1: false,
    base6: '',
    uptype: '',
    red: '',
    start: '',
    tabBar: '',
    id: '',
    select: false,
    tihuoWay: '',
    array: '',
    array1: '',
    index: '',
    shoptype: '1',
    shop_id: '',
    arraytext:'',
    shopname1: "",
    arrid:''
  },
  bindShowMsg() {
    this.setData({
      select: !this.data.select
    })
  },
  // 门店确定
  bindPickerChange: function (e) {
    // console.log(this.data.array1[e.detail.value])
    this.setData({
      array: e.detail.value,
      arraytext: this.data.array1[e.detail.value].info,
      arrid: this.data.array1[e.detail.value]
    })
    // console.log(this.data.array1[e.detail.value].id)
    var data={
      "day": this.data.startTime
    }
   
    this.dateinterface1(data)
   var that=this
    setTimeout(function () {
      // that.dateinterface(data)
      if (that.data.array1[e.detail.value].info=='所有'){

        
        var data_items = that.data.uptype
        for (var k = 0; k < data_items.length; k++) {
          for (var x = 0; x < that.data.array1.length; x++) {

            if (data_items[k].alarm_value == that.data.array1[x].id) {
              data_items[k].alarm_value = that.data.array1[x].info
            }
          }
        }
        // this.dataitems(data_items)
        // console.log(data_items)

        
        that.dataitems(data_items)
      }else{
        var data_items = that.data.uptype

        for (var k = 0; k < data_items.length; k++) {

          if (data_items[k].alarm_value == that.data.arrid.id) {

            // type.push()  
            data_items[k].alarm_value = that.data.arrid.info

          }
        }
        var type = []
        for (var i = 0; i < data_items.length; i++) {

          if ((typeof data_items[i].alarm_value) == 'string') {
            type.push(data_items[i])
          }
        }
        that.dataitems(type)
      }
      
   
    }, 100)  
  },
 
  //给点击的日期设置一个背景颜色
  bindDateChange3: function (e) {
    var data = {
      "day": e.detail.value
    }
    // console.log(e)
    this.dateinterface(data)

    this.setData({
      startTime: e.detail.value,
      type: '1'
    })
  },

  


  //  请求日期接口
  dateinterface(data) {
    axios.post('mall_manager/get_device_exception_info', data, this.shuffleSuc);
  },
  dateinterface1(data) {
    axios.post('mall_manager/get_device_exception_info', data, this.shuffleSuc2);
  },
  //  请求门店
  shoplist(data) {
    axios.getData('mall_manager/get_alarm_list', this.shoplist1);
  },
  shoplist1(res) {
    //  console.log(res.data.data)
    var shoulist = res.data.data
   
    var list = shoulist.unshift({ info: "所有", group: "", id: "" })
    
    this.setData({
     
      array1: shoulist
    })
  },
  shuffleSuc2(res){
  
    this.setData({
      uptype: res.data.data
    })
  },
  
  // 得到当前日期
  double(num) {//让单数变成双数
    if (num < 10) {
      return '0' + num;//单数前面添加0
    } else {
      return num;
    }
  },
  time() {
    var that = this;
    var date = new Date();//创建一个日期对象
    var year = date.getFullYear()//获取4位年份
    var month = date.getMonth() + 1;//+1
    var day = date.getDate();
    var str = '';
    var hour = date.getHours();
    var min = date.getMinutes();
    var sec = date.getSeconds();
    str = year + '-' + this.double(month) + '-' + this.double(day)
    this.setData({
      newday: str
    })

    this.setData({
      endTime: str,
      startTime: str
    })
    var data = {
      "day": str,
     
    }
    setTimeout(function () {
      // that.dateinterface(data)
      that.shoplist()
    }, 100)
    // 最大选择90天数据
    var date1 = new Date();
    var date2 = new Date(date1);
    date2.setDate(date1.getDate() - 90);
    var state = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate()
    this.setData({
      start: state

    })
    // console.log(state);
  },
  addrevamp:function(e){
    // console.log(e)
  },
  // 下拉刷新
  onPullDownRefresh: function (a) {
    // console.log(this.data.shop_id)
    var data = {
      "end_day": this.data.endTime,
      "start_day": this.data.startTime,
      "shop_id": this.data.shop_id
    }
    this.dateinterface(data)
    wx.stopPullDownRefresh()
  },

  onLoad: function (id) {
    // 请求门店
    this.time()
    app.editTabBar()
    // console.log(app)
    this.setData({
      tabBar: app,
     
    })

  },
  shuffleSuc(res) {
    if (res.data.code == 'E000000') {
      //  console.log(res.data.data.items)
      var data_items = res.data.data
      // this.setData({
      //   data: res.data.data
      // })
     
      //  console.log(this.data.arrid)
      if (data_items.length>0){
        if (this.data.arrid == '' || this.data.arrid === null || this.data.arrid===undefined){
          for (var k = 0; k < data_items.length; k++) {
            for (var x = 0; x < this.data.array1.length; x++) {

              if (data_items[k].alarm_value == this.data.array1[x].id) {
                data_items[k].alarm_value = this.data.array1[x].info
              }
            }
          }
          this.dataitems(data_items)
        }else{
          if (this.data.arrid.info=='所有'){
            for (var k = 0; k < data_items.length; k++) {
              for (var x = 0; x < this.data.array1.length; x++) {

                if (data_items[k].alarm_value == this.data.array1[x].id) {
                  data_items[k].alarm_value = this.data.array1[x].info
                }
              }
            }
            this.dataitems(data_items)
          }else{

         

          var type = []
          
          for (var k = 0; k < data_items.length; k++) {
         
            if (data_items[k].alarm_value == this.data.arrid.id) {
            
              // type.push()  
              data_items[k].alarm_value = this.data.arrid.info 
              
            }
          }
          
          for (var i = 0; i < data_items.length;i++){
            
            if ((typeof data_items[i].alarm_value)=='string'){
              type.push(data_items[i])
            }
          }         
          this.dataitems(type)
          }
        }

        

        this.setData({
          shopname1: res.data.data
        })
        
      }else{
        wx.showLoading({
          title: '暂无数据',
          mask: true
        })
        setTimeout(() => {
          wx.hideLoading()
        }, 500)
      }

    } else {
     
    }
    // console.log(res)
  },
  dataitems: function (data_items){
    for (var i = 0; i < data_items.length; i++) {
      for (var j = i + 1; j < data_items.length; j++) {
        if (data_items[i].equipment_id == data_items[j].equipment_id) {
          data_items[i].alarm_value += ("," + data_items[j].alarm_value)
          data_items.splice(j, 1)
          j--
        }
      }
    }
    this.setData({
      data: data_items
    })
  }
})
